package com.imooc.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @author wangke
 * @version 1.0.0
 * @Description 全局跨域配置
 * @createTime 2021年07月10日 12:04:00
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter(){
        CorsConfiguration config = new CorsConfiguration();
        //允许请求的头
        config.addAllowedHeader("*");
        //允许请求的方法
        config.addAllowedMethod("*");
        //是否可以携带cookies 等信息
        config.setAllowCredentials(true);
        //允许跨域访问的地址
        config.addAllowedOrigin("http://localhost:8080");

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        //注册 拦截规则，和跨域配置
        source.registerCorsConfiguration("/**",config);

        return  new CorsFilter(source);

    }
}
